/**
 * 
 */
package com.auth.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;


import com.action.AbstractAction;
import com.auth.model.Account;
import com.auth.model.Role;
import com.auth.service.AccountService;
import com.auth.service.AccountServiceImpl;
import com.manage.cache.ChannelManager;
import com.manage.cache.CpInfoManager;
import com.manage.model.Channel;
import com.manage.model.CpInfo;
import com.manage.cache.SpInfoManager;
import com.manage.model.SpInfo;

public class AccountAction extends AbstractAction
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger logger;
	private AccountService service;
	
	public AccountAction()
	{
		logger = Logger.getLogger(this.getClass());
		service = new AccountServiceImpl();
	}
	
	public String execute()
	{
		return SUCCESS;
	}

	private List<Account> accounts;
	private List<Role>    roles;
	private Account       account;
	private String        type;
	private List<Integer> merchantIds;
	private List<Integer> spIds;
	private List<Integer> channelIds;
	private List<Integer> cpIds;
	private List<SpInfo> spInfos;
	private List<Channel> channels;
	private List<CpInfo> cpInfos;
	
	public String list()
	{
		logger.debug("list account");
		accounts = service.getAccounts();
		roles = service.getRoles();
		return SUCCESS;
	}
	
	public String show()
	{
		if (account.getId() != null && account.getId().length() > 0)
		{
			account = service.getAccount(account.getId());
			type = "update";
			merchantIds = service.getAccountMerchant(account.getId());
			spIds = service.getAccountSp(account.getId());
			channelIds = service.getAccountChannel(account.getId());
			cpIds = service.getAccountCp(account.getId());
		}
		else
		{
			account.setId("");
			account.setPassword(com.utils.security.PasswordGenerator.generate(8));
			type = "insert";
			merchantIds = new ArrayList<Integer>();
			spIds = new ArrayList<Integer>();
			channelIds = new ArrayList<Integer>();
			cpIds = new ArrayList<Integer>();
		}

		channels = ChannelManager.getInstance().getChannels();
		spInfos = SpInfoManager.getInstance().getSpInfos();
		cpInfos = CpInfoManager.getInstance().getCpInfos();
		roles = service.getRoles();
		return SUCCESS;
	}
	
	public String save()
	{
		service.saveAccount(operator, type, account);
		service.saveAccountMerchant(operator, account.getId(), merchantIds);
		service.saveAccountSp(operator, account.getId(), spIds);
		service.saveAccountChannel(operator, account.getId(), channelIds);
		service.saveAccountCp(operator, account.getId(), cpIds);
		return SUCCESS;
	}
	
	private String oldPassword;
	private String newPassword;
//	private String confirmPassword;	
	
	public String password_change()
	{
		if (operator == null)
		{
			addActionError("请重新登录");
			return INPUT;
		}
		
		if (account == null || account.getId() == null || account.getId().length() == 0)
			account = operator;
		else
			account = service.getAccount(account.getId());
		
		return SUCCESS;
	}
	
	public String password_save()
	{
		if (operator == null)
		{
			addActionError("请重新登录");
			return INPUT;
		}

		Account act = service.getAccount(account.getId());
		if (operator.getId().equals("admin")
				|| service.encrypt(oldPassword).equals(act.getPassword()))
		{
			act.setPassword(newPassword);
			service.updatePassword(operator, account.getId(), newPassword);
			// setSession("account", act);
			if (act.getId().equals(operator.getId()))
			{
				operator = service.getAccount(act.getId());
				session.setAttribute("account", operator);
			}
		}
		else
		{
			addActionError("密码输入错误");
		}
		return SUCCESS;
	}

	/**
	 * @return the roles
	 */
	public List<Role> getRoles() {
		return roles;
	}

	/**
	 * @param roles the roles to set
	 */
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	/**
	 * @return the accounts
	 */                 
	public List<Account> getAccounts() {
		return accounts;
	}

	/**
	 * @param accounts the accounts to set
	 */
	public void setAccounts(List<Account> accounts) {
		this.accounts = accounts;
	}

	/**
	 * @return the account
	 */
	public Account getAccount() {
		return account;
	}

	/**
	 * @param account the account to set
	 */
	public void setAccount(Account account) {
		this.account = account;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @return the oldPassword
	 */
	public String getOldPassword() {
		return oldPassword;
	}

	/**
	 * @param oldPassword the oldPassword to set
	 */
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	/**
	 * @return the newPassword
	 */
	public String getNewPassword() {
		return newPassword;
	}

	/**
	 * @param newPassword the newPassword to set
	 */
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	/**
	 * @return the merchantIds
	 */
	public List<Integer> getMerchantIds() {
		return merchantIds;
	}

	/**
	 * @param merchantIds the merchantIds to set
	 */
	public void setMerchantIds(List<Integer> merchantIds) {
		this.merchantIds = merchantIds;
	}

	/**
	 * @return the spIds
	 */
	public List<Integer> getSpIds() {
		return spIds;
	}

	/**
	 * @param spIds the spIds to set
	 */
	public void setSpIds(List<Integer> spIds) {
		this.spIds = spIds;
	}

	/**
	 * @return the channelIds
	 */
	public List<Integer> getChannelIds() {
		return channelIds;
	}

	/**
	 * @param channelIds the channelIds to set
	 */
	public void setChannelIds(List<Integer> channelIds) {
		this.channelIds = channelIds;
	}

	/**
	 * @return the spInfos
	 */
	public List<SpInfo> getSpInfos() {
		return spInfos;
	}

	/**
	 * @param spInfos the spInfos to set
	 */
	public void setSpInfos(List<SpInfo> spInfos) {
		this.spInfos = spInfos;
	}

	/**
	 * @return the channels
	 */
	public List<Channel> getChannels() {
		return channels;
	}

	/**
	 * @param channels the channels to set
	 */
	public void setChannels(List<Channel> channels) {
		this.channels = channels;
	}

	public List<Integer> getCpIds() {
		return cpIds;
	}

	public void setCpIds(List<Integer> cpIds) {
		this.cpIds = cpIds;
	}

	public List<CpInfo> getCpInfos() {
		return cpInfos;
	}

	public void setCpInfos(List<CpInfo> cpInfos) {
		this.cpInfos = cpInfos;
	}
	
	
}
